package org.teamh.gui.login;

import static org.lwjgl.opengl.GL11.*;

import java.util.Timer;

import org.teamh.gui.Event;
import org.teamh.gui.GUI;
import org.teamh.gui.Screen;
import org.teamh.gui.createGame.ErrorPopup;

import de.lessvoid.nifty.builder.EffectBuilder;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.button.builder.ButtonBuilder;
import de.lessvoid.nifty.controls.textfield.builder.TextFieldBuilder;
import de.lessvoid.nifty.effects.EffectEventId;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.tools.Color;

public class LoginScreen implements Screen{
	private GUI gui;
	
	private de.lessvoid.nifty.screen.Screen screen;
	private LoginController loginController;
	private ErrorPopup errorPopup;
	private String currentBackground1024= "res/background/1_1024.jpg";
	private Timer backGroundTimer;
	private ImageBuilder imageBuilder;
	private int seconds=3;

	public LoginScreen(GUI gui){
		
		this.gui = gui;
	}
	
	private void timer() {
		backGroundTimer= new Timer();
		backGroundTimer.schedule(new Background(this), 0, seconds*1000); 
	}

	
	@Override
	public void build() {
		
		loginController = new LoginController(gui);

		screen = new ScreenBuilder("loginScreen") {{
			
			controller(new LoginController(gui));
			layer(new LayerBuilder() {{
				childLayoutCenter();
				imageBuilder = new ImageBuilder("background") {{
				height("768");
				width("1024");
                filename(currentBackground1024);
                
                onStartScreenEffect(new EffectBuilder("fade") {{
                	effectParameter("start", "#00");
                	effectParameter("end", "#ff");
                	length(500);
                }});
                onEndScreenEffect(new EffectBuilder("fade") {{
                	effectParameter("start", "#ff");
                	effectParameter("end", "#00");
                	length(500);
                }});
			}};
			image(imageBuilder);
				
				/*image(new ImageBuilder("background") {{
					height("768");
					width("1024");
                    filename(currentBackground1024);	
//					height("600");
//					width("800");
//					filename("res/background/background800mal600.jpg");
                    
                    onStartScreenEffect(new EffectBuilder("fade") {{
                    	effectParameter("start", "#00");
                    	effectParameter("end", "#ff");
                    	length(500);
                    }});
                    
                    onEndScreenEffect(new EffectBuilder("fade") {{
                    	effectParameter("start", "#ff");
                    	effectParameter("end", "#00");
                    	length(500);
                    }});
				}});*/
				/*
				//Hilfedatei
				panel(new PanelBuilder() {{	
					backgroundColor("1,1,1,0.6f");
					childLayoutAbsolute();
					height("400");
					width("400");	
					text(new TextBuilder(){{
						text("Hilfedateitext");	
					}});
				}});
				//Ende Hilfedatei
				 * 
				 */
				panel(new PanelBuilder("outerouterLoginPanel") {{
					//paddingTop("100");
					paddingTop("80");
					paddingLeft("30");
					alignCenter();
					valignTop();
					width("300");
					height("350");
					childLayoutVertical();
					panel(new PanelBuilder("outerLoginPanel") {{						
						interactOnClick("hideAllTexfieldEffects()");//Unsichtbarmachen aller Textfeld-Effekte
						onCustomEffect(new EffectBuilder("squizzle") {{
							effectParameter("offset", "10");
							length(300);
							startDelay(0);
							inherit(true);
						}});
						onStartScreenEffect(new EffectBuilder("move") {{
							effectParameter("mode", "in");
							effectParameter("direction", "left");
							length(500); 
							startDelay(0);
							inherit(true);
							post(false);
						}});
						onEndScreenEffect(new EffectBuilder("move") {{
							effectParameter("mode", "out");
							effectParameter("direction", "right");
							length(500);
							startDelay(0);
							inherit(true);
						}});
						alignCenter();
						valignTop();
						width("300");
						height("350");
						style("nifty-panel");
						childLayoutVertical();
						panel(new PanelBuilder() {{
							childLayoutHorizontal();
							panel(new PanelBuilder() {{ // logo und so
								childLayoutCenter();
								height("120");
								width("100");
								image(new ImageBuilder("logo") {{
									height("96");
									width("76");
									filename("res/img/logo/wasp_edited.png");								
								}});
							}});
							panel(new PanelBuilder() {{
								childLayoutVertical();
								width("180");
								panel(new PanelBuilder() {{
									height("20");
								}});
								panel(new PanelBuilder() {{
									childLayoutCenter();
									color(new Color(1,1,0,1));
									width("180");
									text(new TextBuilder() {{
										font("verdana-small-regular.fnt");
										color(new Color(1,1,1,1));
										text("Wasp Enterprises");
									}});
								}});
								
								panel(new PanelBuilder() {{
									childLayoutCenter();
									color(new Color(1,0,1,1));
									width("172");

									text(new TextBuilder() {{
										font("res/font/aurulent-sans-36.fnt");
										color(new Color(1,1,1,1));
										text("CreepSmash");
									}});
								}});
								
							}});
						}});
						
						
						panel(new PanelBuilder() {{ // Oben
							
							height("40");
							childLayoutHorizontal();
							
							//Effekt Name-Texfeld
							panel(new PanelBuilder() {{ // oben links
								name("LoginNameTextfieldEffect");
								id("LoginNameTextfieldEffect");
								childLayoutAbsoluteInside();
								paddingLeft("50");
								width("0");
								text(new TextBuilder() {{
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("Name");
									onActiveEffect(new EffectBuilder("colorPulsate") {{
					    		        effectParameter("period", "1600");
					    		        effectParameter("startColor", "#00000000");
					    		        effectParameter("endColor", "#ffffffff");
					    				}});	
								}});
								visible(true);
							}});
							//ENDE Effekt Name-Texfeld
							
							panel(new PanelBuilder() {{ // oben links
								name("LoginNameTextfield");
								id("LoginNameTextfield");
								childLayoutAbsoluteInside();
								width("130");
								text(new TextBuilder() {{
									font("verdana-small-regular.fnt");
									color(new Color(1,1,1,1));
									text("Name");
								}});
								visible(true);
							}});
							
							panel(new PanelBuilder() {{ // oben rechts
								childLayoutAbsoluteInside();
								visibleToMouse(true);
								control(new TextFieldBuilder("eingabeName") {{
									width("100");
									font("verdana-small-regular.fnt");
									//Effekt von Nametextfeld aufrufen 
									 interactOnClick("changeVisibility(LoginNameTextfieldEffect,LoginPasswordTextfieldEffect)");//das erste Argument wird sichtbar, das zweite unsichtbar
	                                //ENDE Effekt von Nametextfeld aufrufen 
								}});
							}});
						}});
						panel(new PanelBuilder() {{ // mitte
							childLayoutHorizontal();
							height("40");
							//Effekt Passwort-Texfeld
							panel(new PanelBuilder() {{//mitte links
								name("LoginPasswordTextfieldEffect");
								id("LoginPasswordTextfieldEffect");
								paddingLeft("70");
								childLayoutAbsoluteInside();
								width("0");
								text(new TextBuilder() {{
									font("verdana-small-regular.fnt");
									text("Passwort");
									onActiveEffect(new EffectBuilder("colorPulsate") {{
					    		        effectParameter("period", "1600");
					    		        effectParameter("startColor", "#00000000");
					    		        effectParameter("endColor", "#ffffffff");
					    				}});
								visible(false);	
								}});
							}});
							//ENDE Effekt Passwort-Texfeld
							panel(new PanelBuilder() {{//mitte links
								childLayoutAbsoluteInside();
								width("130");
								text(new TextBuilder() {{
									font("verdana-small-regular.fnt");
									text("Passwort");
									
								}});
							}});
							panel(new PanelBuilder() {{// mitte rechts
								childLayoutAbsoluteInside();
								visibleToMouse(true);
								control(new TextFieldBuilder("eingabePasswort") {{
									passwordChar('*');
									width("100");
									font("verdana-small-regular.fnt");
									//Effekt von Passworttextfeld aufrufen 
									 interactOnClick("changeVisibility(LoginPasswordTextfieldEffect,LoginNameTextfieldEffect)");//das erste Argument wird sichtbar, das zweite unsichtbar
	                                //Ende Effekt von Passworttextfeld  aufrufen 
								}});
							}});
						}});
						panel(new PanelBuilder() {{		//falscher login
							width("270");
							childLayoutCenter();
							text(new TextBuilder("wrongLogin") {{
								font("verdana-small-regular.fnt");
								color(new Color(1,0,0,1));
								text("wrong login!");
								visible(false);
							}});
						}});
						panel(new PanelBuilder() {{
							width("270");
							height("50");
							visibleToMouse(true);
							childLayoutCenter();
							control(new ButtonBuilder("mapEditorButton", "MAP EDITOR") {{
								interactOnClick("switchToEditor()");
								alignCenter();
								visibleToMouse(true);
							}});
						}});
						
						
						panel(new PanelBuilder() {{
							childLayoutHorizontal();
							width("270");
							height("45");
							visibleToMouse(true);
							panel(new PanelBuilder(){{
								width("130");
								visibleToMouse(true);
								childLayoutCenter();
								control(new ButtonBuilder("exitButton", "EXIT") {{
									valignCenter();
									interactOnClick("exit()");
									visibleToMouse(true);
								}});
							}});
							panel(new PanelBuilder(){{
								width("130");
								visibleToMouse(true);
								childLayoutCenter();
								control(new ButtonBuilder("loginButton", "LOGIN") {{
									alignCenter();
									interactOnClick("login()");
									visibleToMouse(true);
								}});
							}});
						}});
					}});
			}});
			}});
		}}.build(gui.getNifty());
		
		timer();
		gui.getNifty().addScreen("loginScreen", screen);
		
	}
	
	@Override
	public void init() {}
	
	@Override
	public void render(float timeElapsed) {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		
		//this loop is called any time the scene is rendered. 
	}

	@Override
	public void keyPressed(int key) {
		
	}

	@Override
	public void keyReleased(int key) {
		
	}

	@Override
	public void mousePressed(int posX, int posY, int button) {
		
	}

	@Override
	public void mouseReleased(int posX, int posY, int button) {
		
	}

	@Override
	public void mouseMoved(int posX, int posY, int button) {
		
	}

	@Override
	public void mouseDragged(int posX, int posY, int button) {
		
	}

	@Override
	public void mouseWheelMoved(int movement) {
		
	}

	@Override
	public void dealloc() {
		
	}

	@Override
	public void eventOccured(Event event) {
		if(event.getKey().equals("login")) {
			boolean status = Boolean.parseBoolean(event.getValue());
			
			if(status) {
				gui.setFollowingScreen(gui.getLobbyScreen());
				gui.getStart().getConnection().sendMessage("LIST MAPS");
			} else {
				Element element = screen.findElementByName("outerLoginPanel");
				element.startEffect(EffectEventId.onCustom);
			}
			screen.findElementByName("wrongLogin").setVisible(!status);
			
		} else if (event.getKey().equals("timeout")) {
			getLoginController().showErrorPopup();
		}
	}
	
	public boolean canPoll() {
		return true;
	}

	public LoginController getLoginController() {
		return loginController;
	}
	
	public void showErrorPopup() {
		gui.getNifty().showPopup(gui.getNifty().getScreen("loginScreen"),
				errorPopup.getErrorPopup().getId(), null);
	}

	public void closeErrorPopup() {
		gui.getNifty().closePopup(errorPopup.getErrorPopup().getId());
	}

	public void setCurrentBackground1024(NiftyImage currentBackground1024) {
		screen.findElementByName("background").getRenderer(ImageRenderer.class).setImage(currentBackground1024);
	}
	
	public Timer getBackGroundTimer() {
		return backGroundTimer;
	}

	public void setBackGroundTimer(Timer backGroundTimer) {
		this.backGroundTimer = backGroundTimer;
	}

	public GUI getGui() {
		return gui;
	}

	@Override
	public String getNiftyScreenID() {
		return "loginScreen";
	}
}
